package org.darcy.controller.privilege;

import java.util.List;

import org.darcy.controller.BaseController;
import org.darcy.entity.privilege.SysRole;
import org.darcy.entity.privilege.SysUser;
import org.darcy.framework.model.PageData;
import org.darcy.framework.model.PageParam;
import org.darcy.framework.model.R;
import org.darcy.service.privilege.SysUserRoleService;
import org.darcy.service.privilege.SysUserService;
import org.darcy.vo.CheckExistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;

@Tag(name = "SysUserController接口：用户管理")
@RestController
@RequestMapping("/privilege/user")
@Slf4j
public class SysUserController extends BaseController<SysUser> {

	private final String info_prefix = "[用户模块]";

	@Autowired
	private SysUserService userService;

	@Autowired
	private SysUserRoleService userGroupService;

	@Operation(summary = "用户分页列表")
	@PostMapping(value = "list")
	public R list(@RequestBody PageParam<SysUser> param) {
		log.debug("{}-列表数据", info_prefix);
		PageData<SysUser> data = userService.page(param);
		return R.ok().data(data);
	}

	@Operation(summary = "用户详情")
	@GetMapping("/view/{id}")
	public R view(@PathVariable Integer id) {
		log.debug("{}-查看用户信息", info_prefix);
		SysUser data = userService.getById(id);
		return R.ok().data(data);
	}

	@Operation(summary = "新增用户")
	@PostMapping("add")
	public R add(@RequestBody SysUser record) {
		log.debug("{}-新增SysUser: {}", info_prefix, record);
		userService.insert(record);
		return R.ok("新增成功").data(record);
	}

	@Operation(summary = "根据ID删除用户")
	@PostMapping("/del/{id}")
	public R del(@PathVariable Integer id) {
		log.debug("{}-根据ID删除", info_prefix, id);
		userService.delete(id);
		return R.ok("删除成功");
	}

	@Operation(summary = "批量删除用户")
	@PostMapping("/delBatch")
	public R delBatch(@RequestBody List<Integer> ids) {
		userService.deleteByIds(ids);
		return R.ok("批量删除用户成功");
	}

	@Operation(summary = "更新用户")
	@PostMapping("update")
	public R update(@RequestBody SysUser record) {
		log.debug("{}-保存信息: {}", info_prefix, record);
		userService.update(record);
		return R.ok("更新成功");
	}

	@Operation(summary = "查看属性")
	@GetMapping("/model")
	public SysUser model() {
		return null;
	}

	@Operation(summary = "判断字段是否存在")
	@PostMapping(value = "/checkEntityExist")
	protected R checkEntityExist(@RequestBody CheckExistVo checkExistVo) {
		return this.checkExist(userService, checkExistVo);
	}

	@Operation(summary = "用户分配角色")
	@PostMapping("/addUserRole/{userId}")
	public R allocationRoles(@PathVariable Integer userId, @RequestBody List<SysRole> groups) {
		userGroupService.addUserRole(userId, groups, this.getLoginUser());
		return R.ok("用户分配角色成功");
	}

	@Operation(summary = "设置密码")
	@PostMapping(value = "/setPassword")
	public R setPassword(@RequestBody SysUser user) {
		userService.setPassword(user);
		return R.ok("设置密码成功");
	}

	@Operation(summary = "获取所有用户")
	@PostMapping(value = "/getAll")
	public R getAll(@RequestBody SysUser user) {
		List<SysUser> list = userService.getAll(user);
		return R.ok().data(list);
	}

}
